//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DMinus.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/TridiagonalOperator.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::CDMinus::CDMinus (UInt64 gridPoints, Double h) : CTridiagonalOperator(CDMinus::typeid)
{
    try
    {
#ifdef HANDLE
        _phDMinus = NULL;
#endif
        QuantLib::Size _gridPoints = (QuantLib::Size)ValueHelper::Convert (gridPoints); //d
        QuantLib::Real _h = (QuantLib::Real)ValueHelper::Convert (h); //d
        _ppDMinus = new boost::shared_ptr<QuantLib::DMinus> (new QuantLib::DMinus ( _gridPoints,  _h ));
        SetTridiagonalOperator (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDMinus));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::CDMinus::CDMinus (boost::shared_ptr<QuantLib::DMinus>& childNative, Object^ owner) : CTridiagonalOperator(CDMinus::typeid)
{
#ifdef HANDLE
	_phDMinus = NULL;
#endif
	_ppDMinus = &childNative;
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDMinus));
}
Cephei::QL::Methods::Finitedifferences::CDMinus::CDMinus (QuantLib::DMinus& childNative, Object^ owner) : CTridiagonalOperator(CDMinus::typeid)
{
#ifdef HANDLE
	_phDMinus = NULL;
#endif
	_ppDMinus = new boost::shared_ptr<QuantLib::DMinus> (&childNative);
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDMinus));
    _DMinusOwner = owner;
    _TridiagonalOperatorOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::CDMinus::CDMinus (CDMinus^ copy) : CTridiagonalOperator(CDMinus::typeid)
{
#ifdef HANDLE
	_phDMinus = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDMinus = new boost::shared_ptr<QuantLib::DMinus> (copy->GetShared());
        _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDMinus));
    }
}
Cephei::QL::Methods::Finitedifferences::CDMinus::CDMinus (PLATFORM::Type^ t) : CTridiagonalOperator(CDMinus::typeid)
{
#ifdef HANDLE
	_phDMinus = NULL;
#endif
	if (!t->IsSubclassOf(CDMinus::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::CDMinus::CDMinus (QuantLib::Handle<QuantLib::DMinus>& childNative, Object^ owner)  : CTridiagonalOperator(CDMinus::typeid)
{
	_phDMinus = &childNative;
	_ppDMinus = &static_cast<boost::shared_ptr<QuantLib::DMinus>>(childNative.currentLink());
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDMinus));
    _DMinusOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::CDMinus::CDMinus (QuantLib::Handle<QuantLib::DMinus> childNative)  : CTridiagonalOperator(CDMinus::typeid)
{
	_phDMinus = &childNative;
	_ppDMinus = &static_cast<boost::shared_ptr<QuantLib::DMinus>>(childNative.currentLink());
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDMinus));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::CDMinus::CDMinus (QuantLib::DMinus childNative)  : CTridiagonalOperator(CDMinus::typeid)
{
#ifdef HANDLE
	_phDMinus = NULL;
#endif
	_ppDMinus = new boost::shared_ptr<QuantLib::DMinus> (new QuantLib::DMinus (childNative));
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDMinus));
}
#endif

Cephei::QL::Methods::Finitedifferences::CDMinus::~CDMinus ()
{
    if (_ppDMinus != NULL)
    {
	    delete _ppDMinus;
        _ppDMinus = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::CDMinus::!CDMinus ()
{
    if (_ppDMinus != NULL)
    {
	    delete _ppDMinus;
    }
}
QuantLib::DMinus& Cephei::QL::Methods::Finitedifferences::CDMinus::GetReference ()
{
    if (_ppDMinus == NULL) throw REFNEW NativeNullException ();
	return **_ppDMinus;
}
boost::shared_ptr<QuantLib::DMinus>& Cephei::QL::Methods::Finitedifferences::CDMinus::GetShared ()
{
    if (_ppDMinus == NULL) throw REFNEW NativeNullException ();
	return *_ppDMinus;
}
QuantLib::DMinus* Cephei::QL::Methods::Finitedifferences::CDMinus::GetPointer ()
{
    if (_ppDMinus == NULL) throw REFNEW NativeNullException ();
	return &**_ppDMinus;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DMinus>& Cephei::QL::Methods::Finitedifferences::CDMinus::GetHandle ()
{
	if (_phDMinus == NULL)
	{
		_phDMinus = new Handle<QuantLib::DMinus> (*_ppDMinus);
	}
	return *_phDMinus;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::CDMinus::HasNative () 
{
	return (_ppDMinus != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::IDMinus^ Cephei::QL::Methods::Finitedifferences::CDMinus_Factory::Create (UInt64 gridPoints, Double h)
{
    return REFNEW CDMinus ( gridPoints,  h);
}
